package com.day3.t2;

public class Main {
    public String longestPalindrome(String s) {
        int n=s.length();
        if(n<2)return s;
        int max=1,start=0;
        boolean[][] dp=new boolean[n][n];
        for (int i = 0; i < n; i++){
            dp[i][i]=true;
        }
        char[] chars=s.toCharArray();
        for(int len=2;len<=n;len++){
            for(int i=0;i<n-len+1;i++){
                int j=i+len-1;
                if(chars[i]!=chars[j]){
                    dp[i][j]=false;
                }else{
                    if(len<3){
                        dp[i][j]=true;
                    }else{
                        dp[i][j]=dp[i+1][j-1];
                    }
                }
                if(dp[i][j]&&len>max){
                    max=len;
                    start=i;
                }
            }
        }
        return s.substring(start,start+max);
    }
}

